#include "StringOps.h"
#include<sstream>

using namespace std;

std::string stringify(double x)
{
   std::ostringstream o;
   if (!(o << x))
     throw "stringify(double)";
   return o.str();
} 

string& subtract(string& s1, string& s2)
{
	s1.replace(0,s2.length(),s2);
	return s1;
}

string& trim(string& in_rString)
{
    // Trim Both leading and trailing spaces  
    size_t startpos = in_rString.find_first_not_of(" \t"); 
    // Find the first character position after excluding leading blank spaces  
    size_t endpos = in_rString.find_last_not_of(" \t"); 
    // Find the first character position from reverse af  
       
    // if all spaces or empty return an empty string  
    if(( string::npos == startpos ) || ( string::npos == endpos))  
        in_rString = "";  
    else
        in_rString = in_rString.substr(startpos, endpos-startpos+1 ); 

    return in_rString;
}

void splitAsNameAndType(string& in_sString, string& in_sName, string& in_sType)
{
    int nLen;
    unsigned int nSplitPos;

    trim(in_sString);
    nSplitPos = in_sString.find_last_of("\t ");
    nLen = in_sString.size();

    if(string::npos == nSplitPos)
        in_sName = in_sString;
    else
    {
        in_sName = in_sString.substr(nSplitPos, nLen -nSplitPos);
        in_sType = in_sString.substr(0,nSplitPos);
    }

    trim(in_sName);
    trim(in_sType);
}

void copyFromFirstInstanceOf(string& in_sString, string& in_sFind, string& out_sResult)
{
    int nLen;
    unsigned int nSplitPos;

    trim(in_sString);
    nSplitPos = in_sString.find(in_sFind);
    nLen = in_sString.size();

    if(string::npos == nSplitPos)
        out_sResult = in_sString;
    else
    {
		int nCopyStartPos = nSplitPos + in_sFind.length();
		out_sResult = in_sString.substr(nCopyStartPos , nLen - nCopyStartPos);
    }

	trim(out_sResult);
}

void copyTillFirstInstanceOf(string& in_sString, string& in_sFind, string& out_sResult)
{
    int nLen;
    unsigned int nSplitPos;

    trim(in_sString);
    nSplitPos = in_sString.find(in_sFind);
    nLen = in_sString.size();

    if(string::npos == nSplitPos)
        out_sResult = in_sString;
    else
		out_sResult = in_sString.substr(0 ,nSplitPos);

	trim(out_sResult);
}

string getContentsOf(string sTarget, string sStarters, string sEnders)
{
	string sOutput = "";

    size_t startpos = sTarget.find_first_of(sStarters); 
    // Find the first character position after excluding leading blank spaces  
	size_t endpos = sTarget.find_last_of(sEnders); 
    // Find the first character position from reverse af  
       
    // if all spaces or empty return an empty string  
    if(( string::npos == startpos ) || ( string::npos == endpos))  
        sOutput = "";  
    else
        sOutput = sTarget.substr(startpos+1, endpos-startpos-1 ); 

    return sOutput;
}

string getLastWord(string sTarget)
{
	string sOutput;
	
	trim(sTarget);

    // Trim Both leading and trailing spaces  
    size_t startpos = sTarget.find_last_of(" \t"); 
       
    // if all spaces or empty return an empty string  
    if( string::npos == startpos)
        sOutput = "";  
    else
		sOutput = sTarget.substr(startpos, sTarget.length()-startpos); 

    return sOutput;
}

string getFirstWord(string sTarget)
{
	string sOutput;
	
	trim(sTarget);

    // Trim Both leading and trailing spaces  
    size_t endpos = sTarget.find_first_of(" \t"); 
       
    // if all spaces or empty return an empty string  
    if( string::npos == endpos)
        sOutput = "";  
    else
        sOutput = sTarget.substr(0, endpos); 

    return sOutput;
}

bool isWhitespace(string sCheck)
{
	bool bResult = true;
	for(size_t i=0;i<sCheck.length();i++)
	{
		switch(sCheck[i])
		{
		case ' ':
		case '\t':
		case '\r':
		case '\n':
			break;
		default:
			bResult=false;
		}
	}
	return bResult;
}

string getFilename(string sTarget)
{
	string sOutput;
	
	trim(sTarget);

    // Trim Both leading and trailing spaces  
    size_t startpos = sTarget.find_last_of("\\/"); 
       
    // if all spaces or empty return an empty string  
    if( string::npos == startpos)
        sOutput = sTarget;  
    else
		sOutput = sTarget.substr(startpos+1, sTarget.length()-startpos); 

    return sOutput;
}

string getFilehome(string sTarget)
{
	string sOutput;
	
	trim(sTarget);

    // Trim Both leading and trailing spaces  
    size_t startpos = sTarget.find_last_of("\\/"); 
       
    // if all spaces or empty return an empty string  
    if( string::npos == startpos)
        sOutput = "./";  
    else
		sOutput = sTarget.substr(0, startpos); 

    return sOutput;
}